//
//  FuncationListViewController.swift
//  XingCeHelper
//
//  Created by 申铭 on 2022/10/18.
//

import UIKit
import SVProgressHUD

class FuncationListViewController: RoundCornerTableViewController {

    private var datas = [BaseSectionModel]()
    private var viewModel: FuncationListViewModel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "功能列表"
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 70
        tableView.sectionFooterHeight = 2
        tableView.register(UINib(nibName: kSettingArrowAndExplainCell, bundle: nil), forCellReuseIdentifier: kSettingArrowAndExplainCell)
 
        viewModel = FuncationListViewModel()
        datas = viewModel.loadDatas()
    }
}

extension FuncationListViewController {
    func numberOfSections(in tableView: UITableView) -> Int {
        return datas.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas[section].items.count
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 20))
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 10))
        return view
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellModel = datas[indexPath.section].items[indexPath.row]
        if cellModel is SettingArrowAndExplainCellModel {
            let cell = tableView.dequeueReusableCell(withIdentifier: kSettingArrowAndExplainCell, for: indexPath) as! SettingArrowAndExplainCell
            cell.model = cellModel as? SettingArrowAndExplainCellModel
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: kSettingArrowAndExplainCell, for: indexPath) as! SettingArrowAndExplainCell
            return cell
        }
    }
    
//    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
//        let cornerCell = cell as! RoundCornerCell
//        
//        // 清除状态，否则复用是会有问题
//        cornerCell.layer.mask = nil
//        cornerCell.rectCorner = []
//        let row = indexPath.row
//        // 最后一行
//        let lastRow = tableView.numberOfRows(inSection: indexPath.section) - 1
//        // 如果是第一行也是最后一行,只有一个cell四个圆角
//        if row == 0 && row == lastRow {
//            cornerCell.rectCorner = .allCorners
//        } else if row == 0 { // 第一行
//            cornerCell.rectCorner = [.topLeft, .topRight]
//        } else if row == lastRow { // 最后一行
//            cornerCell.rectCorner = [.bottomLeft, .bottomRight]
//        }
//    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        
        let cellModel = datas[indexPath.section].items[indexPath.row]
        if cellModel is BaseSettingCellModel {
            let model = cellModel as! BaseSettingCellModel
            if model.tag == Function.showIdiom.rawValue {
                if let path = Bundle.main.path(forResource: "成语", ofType: "txt") {
                    let wk = WKWebViewController()
                    wk.load(path)
                    wk.canSave = true
                    FCurrentNav()?.pushViewController(wk, animated: true)
                }
            } else if model.tag == Function.importIdiom.rawValue {
                SVProgressHUD.show()
                ImportTool.importIdiom { flag in
                    if flag {
                        SVProgressHUD.dismiss()
                        ImportResultController().push()
                        NotificationCenter.default.post(name: .refreshReciteViewController, object: nil, userInfo: nil)
                    } else {
                        SVProgressHUD.showError(withStatus: "导入失败")
                    }
                }
            } else if model.tag == Function.importIdiomExplain.rawValue {
                UrlTool.getUrl(.idiomExplain).openUrl()
            } else if model.tag == Function.importIdiomByCopy.rawValue {
                let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "importidiombytextview") as! ImportIdiomByTextViewController
                navigationController?.pushViewController(vc, animated: true)
            } else if model.tag == Function.importShenLunByXlsx.rawValue {
                ImportTool.importShenLun()
            } else if model.tag == Function.importShenLunExplain.rawValue {
                
            } else if model.tag == Function.showShenLun.rawValue {
                if let path = Bundle.main.path(forResource: "申论规范用词", ofType: "xlsx") {
                    let wk = WKWebViewController()
                    wk.load(path)
                    wk.canSave = true
                    FCurrentNav()?.pushViewController(wk, animated: true)
                }
            } else if model.tag == Function.importShenLun2ByXlsx.rawValue {
                ImportTool.importShenLun2()
            }
        }
    }
}
